#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Run a simple backup of the Bacula build directory using the compressed option
#   then backup four times, each with incremental then finally restore.
#   It should require at least 4 different bsrs.
#
TestName="four-jobs-test"
JobName=SpanVol
. scripts/functions

scripts/cleanup
scripts/copy-test-confs
echo "${cwd}/build" >${cwd}/tmp/file-list

change_jobname CompressedTest $JobName
start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
estimate job=$JobName listing
estimate job=$JobName
estimate job=$JobName
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File
# make some files for the incremental to pick up
touch ${cwd}/build/src/dird/*.c ${cwd}/build/src/dird/*.o
touch ${cwd}/build/src/lib/*.c ${cwd}/build/src/lib/*.o
   
#
# run a second job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out  ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

touch ${cwd}/build/src/dird/*.c
touch ${cwd}/build/src/lib/*.c ${cwd}/build/src/lib/*.o
#
# run a third job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

# make some files for the incremental to pick up
touch ${cwd}/build/src/lib/*.c ${cwd}/build/src/lib/*.o
#
# run a fourth job
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File

# 
# now do several restores to ensure we cleanup between jobs
#
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
restore where=${cwd}/tmp/bacula-restores select all storage=File done
yes
wait
restore where=${cwd}/tmp/bacula-restores select all storage=File done
yes
wait
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select storage=File
unmark *
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_bconsole
check_for_zombie_jobs storage=File
stop_bacula

check_two_logs
check_restore_diff
end_test
